var topicId = globalFun.GetQueryString("id");
var shareId = globalFun.GetQueryString("share");
//发表列表    isCollection   0  未收藏
topicId = topicId ? topicId : 1;
var isCollection, shareMsg = {};

fblist(topicId);
Auth();
$('.conent1').click(function() {
	$('.homefw_lbfb').show();
	$('.service_team').hide();
	$('.orderDetailsWai').hide();
	fblist(topicId);
});
//服务项目列表
$('.conent2').click(function() {
	$('.orderDetailsWai').show();
	$('.homefw_lbfb').hide();
	$('.service_team').hide();
	$.ajax({
		type: "post",
		url: apicom + "mobile/after/topic/getTopicDetails",
		data: {
			type: 1,
			page: 0,
			pageSize: 10,
			topicId: topicId
		},
		success: function(res) {
			if (typeof res == "string") {
				var data = JSON.parse(res);
			} else {
				var data = res;
			}
			var content = '';
			if (data.code == 1) {
				$.each(data.content.projectList, function(index, val) {
					content += '<li><div xmid="' + val.id + '" class="service-img"><img src="' + val.imageAddress + '"/><div class="service-mb"><p class="serviceMb-zuo">' + val.title + '</p><p class="serviceMb-you">¥<span  class="service-rmb">' + val.price + '</p></div></div></li>';
				});
			}
			if (data.code == 0) {
				tips(data.msg);
			}
			$("#orderDetails").html(content);
		},
		error: function(err) {
			tips("服务器错误！")
		}
	});
});
//服务团队列表
$('.conent3').click(function() {
	$('.service_team').show();
	$('.orderDetailsWai').hide();
	$('.homefw_lbfb').hide();
	$.ajax({
		type: "post",
		url: apicom + "mobile/after/topic/getTopicDetails",
		data: {
			type: 2,
			page: 0,
			pageSize: 10,
			topicId: topicId
		},
		success: function(res) {
			if (typeof res == "string") {
				var data = JSON.parse(res);
			} else {
				var data = res;
			}
			var content = '';
			if (data.code == 1) {
				$.each(data.content.teamList, function(index, val) {
					content += '<div merchantId="' + val.id + '" class="service_content"><b class="service_pic"><img src="' + val.imageAddress + '"/></b><p class="service_title1">' + val.nike_name + '<span>服务范围 | ' + val.province_name + val.city_name + '</span></p><p class="service_xx">';
					for (var i = 0; i < val.star_num; i++) {
						content += '★';
					}
					content += '</p><p class="service_jj"><span imId="' + val.uuid + '" imName="' + val.nike_name + '" imUrl="' + val.imageAddress + '" class="lianxi_team">联系团队</span><b>' + val.content + '</b></p></div>';
				});
			}
			if (data.code == 0) {
				tips(data.msg);
			}
			$("#service_content").html(content);
		},
		error: function(err) {
			tips("服务器错误！")
		}
	});
});
$(".homefw_lb").on("click", "li", function() {
	$(this).addClass("add_color").siblings().removeClass("add_color");

})

//收藏
$('.navFx-img').click(function() {
	var $this = $(this);
	if (!Auth()) {
		toClientLogin();
	} else {
		if ($(this).hasClass("red")) {
			$.ajax({
				type: "post",
				url: apicom + "mobile/after/topic/collectionTopic",
				data: {
					topicId: topicId,
					uuid: user_info.uuid,
					phone: user_info.phone,
					isCollection: 0
				},
				success: function(res) {
					if (typeof res == "string") {
						var data = JSON.parse(res);
					} else {
						var data = res;
					}
					if (data.code == 1) {
						$this.removeClass("red")
						tips("取消点赞");
					}
					if (data.code == 0) {
						tips(data.msg);
					}
				},
				error: function(err) {
					tips("服务器错误!");
				}
			});
		} else {
			$.ajax({
				type: "post",
				url: apicom + "mobile/after/topic/collectionTopic",
				data: {
					topicId: topicId,
					uuid: user_info.uuid,
					phone: user_info.phone,
					isCollection: 1
				},
				success: function(res) {
					if (typeof res == "string") {
						var data = JSON.parse(res);
					} else {
						var data = res;
					}
					if (data.code == 1) {
						$this.addClass("red")
						tips("点赞成功");
					}
					if (data.code == 0) {
						tips(data.msg);
					}
				},
				error: function(err) {
					tips("服务器错误!");
				}
			});
		}
	}

});

// 返回
function back() {
	if (!globalFun.GetQueryString('from')) {
		window.history.go(-1)
	} else {
		if (mark == 1) {
			setupWebViewJavascriptBridge(function(bridge) {
				bridge.callHandler('goBack', function(resp) {});
			});
		} else if (mark == 0) {
			//var result = lase;
			window.stub.goBack();
			//用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

		}
	}
}

$('.nav1-img').on('click', function() {
	back();
});


//分享
function zhuanfa(data) {
	if (mark === 1) {
		setupWebViewJavascriptBridge(function(bridge) {
			bridge.callHandler('jsToClientShare', data, function(resp) {});
		});
	} else if (mark === 0) {
		//var result = lase;
		data = JSON.stringify(data);
		window.stub.jsToClientShare(data); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

	}
}
$('.navDp-img').on('click', function() {
	//判断是否登陆
	//		if(Auth()){
	//		var data = {
	//			uuid:user_info.uuid,
	//			phone:user_info.phone,
	//			memberId:user_info.id
	//		}
	//		console.log(user_info.id)
	//		$.ajax({
	//			type:"post",
	//		 	url:apicom + "mobile/after/member/memberdetail",
	//		 	data:data,
	//		 	async: false,
	//			dataType: "json",
	//			success:function(res){
	//				if(typeof(res) == 'string'){
	//					res = JSON.parse(res);
	//				}
	//				if(res.content.isHunter == 1){
	//					isHunter = user_info.id
	//
	//				} else{
	//					isHunter = ''
	//				}
	//			},
	//			error:function(err){
	//				console.log(err)
	//			}
	//		})
	//	} else{
	//		sessionStorage.setItem('history', window.location.href);
	//		toClientLogin(location.href)
	//	}

	//		var $self = $(this)
	//		if($self.hasClass("disabled")){
	//			return false
	//		}
	//		$self.addClass("disabled")
	//		setTimeout(function(){
	//			$self.removeClass("disabled")
	//		},1000)

	//		var data = {
	//			type:1,
	//			url:rootdomain +'html/shopCart/shopDetail.html?type=1&id='+id+'&hunter_id='+isHunter+'&merchant_id='+merchant,
	//			title:'拉阿拉啦啦啦啦啦',
	//			content:'是啊a阿发啊发啊啊啊sk；啊',
	//			hunterId:hunterId
	//		}
	//		var share = JSON.stringify(data);
	if (!Auth()) {
		toClientLogin();
	} else {
		shareMsg.url = location.href + "&share=" + user_info.id+"&topicId="+topicId;
		zhuanfa(shareMsg);
	}
});
//课题详情
$.ajax({
	type: "post",
	url: apicom + "mobile/after/topic/getTopicTop",
	data: {
		topicId: topicId,
		memberId: (user_info.id ? user_info.id : "")
	},
	success: function(res) {
		if (typeof res == "string") {
			var data = JSON.parse(res);
		} else {
			var data = res;
		}
		if (data.code == 1) {
			//      	分享信息
			shareMsg.title = data.title;
			shareMsg.type = 1;
			shareMsg.imageAddress = data.imageAddress;
			shareMsg.content = data.msg == null ? "pjyc" : data.msg;
			isCollection = data.isCollection;
			$("#read_num").html(data.readNum == null ? 0 : data.readNum);
			$("#share_num").html(data.serviceTeamNum == null ? 0 : data.serviceTeamNum);
			$("#fb_num").html(data.conNum == null ? 0 : data.conNum);
			$("#box p").html(data.content);
			$("#fw_num").html(data.serviceProjectNum == null ? 0 : data.serviceProjectNum);
			$("#team_num").html(data.serviceTeamNum == null ? 0 : data.serviceTeamNum);
			$("#banner_img").attr("src", data.imageAddress);
			$(".homefw_tq").attr("ktId", data.id);
			$("#kt_title").html(data.title);
			if (isCollection == 0) {
				//      		$(".navFx-img img").attr("src","../../img/d_xin.png");
				$(".navFx-img").removeClass("red");
			} else {
				//      		$(".navFx-img img").attr("src","../../img/d_xin.png");
				$(".navFx-img").addClass("red");
			}
		}
		if (data.code == 0) {
			tips(data.msg);
		}
	},
	error: function(err) {
		tips("服务器错误！");
	}
});



function fblist(topicId) {
	$.ajax({
		type: "post",
		url: apicom + "mobile/after/topic/getTopicDetails",
		data: {
			type: 0,
			page: 0,
			pageSize: 10,
			topicId: topicId
		},
		success: function(res) {
			if (typeof res == "string") {
				var data = JSON.parse(res);
			} else {
				var data = res;
			}
			var content = '';
			if (data.code == 1) {
				$.each(data.content.conList, function(index, val) {
					console.log(getFileName(val.imageList[0]));
					if (getFileName(val.imageList[0]) == ".mp4") {
						content += '<div class="homefw_lbfb"><div fbid="' + val.id + '" class="homefw_lbfb1"><h2>' + val.title + '</h2><p>' + val.description + '</p><div class="home_service-img"><b class="play_btn"></b><video style="width:100%;height:100%;" src="' + val.imageList[0] + '"></video></div></div>';
					} else {
						content += '<div class="homefw_lbfb"><div fbid="' + val.id + '" class="homefw_lbfb1"><h2>' + val.title + '</h2><p>' + val.description + '</p><div class="home_service-img"><img src="' + val.imageList[0] + '"/></div></div>';
					}
					if (!val.serviceProject) {
						content += '';
					} else {
						content += '<div fwid="' + val.serviceProject.id + '" class="homefw_lbfb2"><div class="homefw_lbfb2l"><p>' + val.serviceProject.title + '</p><span>¥' + val.serviceProject.price + '</span><span>' + val.serviceProject.merchantName + '团队提供服务<b></b></span></div><div  class="homefw_lbfb2r"><img src="' + val.serviceProject.imageAddress + '"/></div></div>';
					}
					content += '<div fbid="' + val.id + '" class="homefw_lbfb3"><div class="home_zhuanf"><img src="../../img/a_share.png"/ class="home_share"><span>' + (val.shareNum ? val.shareNum : 0) + '</span></div><div class="home_zhuanf"><img src="../../img/w_hui.png"/ class="home_share"><span >' + (val.commentNum ? val.commentNum : 0) + '</span></div><div class="home_zhuanf"><img src="../../img/a_zan.png"/ class="home_share"><span >' + (val.fabulousNum ? val.fabulousNum : 0) + '</span></div></div></div>';
				});
			}
			if (data.code == 0) {
				tips(data.msg);
			}
			$("#homefw_lbfb").html(content);
		},
		error: function(err) {
			tips("服务器错误！")
		}
	});
}



//跳转服务详情页
$(document).on("click", "#orderDetails .service-img", function() {
	var xmid = $(this).attr("xmid");
	location.href = "home_fwxq.html?id=" + xmid + "&topicId=" + topicId + "&share=" + shareId;
});
//跳转服务团队
$(document).on("click", "#service_content div", function(e) {
	e.stopPropagation();
	sessionStorage.setItem("fromHTML", "H5");
	var tdid = $(this).attr("merchantid");
	location.href = "../service/serviceList.html?id=" + tdid;
});
//资讯详情跳转
$(document).on("click", ".homefw_lbfb1", function() {
	sessionStorage.setItem("fromHTML", "H5");
	var zxid = $(this).attr("fbid");
	location.href = "../shopDetail/zixunxq.html?id=" + zxid;
});
//跳转服务详情页
$(document).on("click", ".homefw_lbfb2", function() {
	var fwid = $(this).attr("fwid");
	location.href = "home_fwxq.html?id=" + fwid + "&topicId=" + topicId + "&share=" + shareId;
});
//资讯详情跳转
$(document).on("click", ".homefw_lbfb3", function() {
	sessionStorage.setItem("fromHTML", "H5");
	var zxid = $(this).attr("fbid");
	location.href = "../shopDetail/zixunxq.html?id=" + zxid;
});

//联系团队
$(document).on("touchend", "#service_content .lianxi_team", function(e) {
	e.stopPropagation();
	if (!Auth()) {
		toClientLogin();
	} else {
		var dataObj = {};
		dataObj.imId = $(this).attr("imid");
		dataObj.imgUrl = $(this).attr("imurl");
		dataObj.nikeName = $(this).attr("imname");
		kefu(dataObj);
	}
});


//获取文件的后缀
function getFileName(o) {
	var filename = o;
	var index1 = filename.lastIndexOf(".");
	var index2 = filename.length;
	var postf = filename.substring(index1, index2); //获取文件后缀名
	return postf;
}



function kefu(dataObj) {
	if (mark === 1) {
		setupWebViewJavascriptBridge(function(bridge) {
			bridge.callHandler('jsCustomerService', dataObj, function(resp) {});
		});
	} else if (mark === 0) {
		dataObj = JSON.stringify(dataObj);
		window.stub.jsCustomerService(dataObj); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。
	}
}



/* 分享回调 */
function shareCallBack(data1) {
	var dataObj = {
		type: 5,
		id: data1,
		phone: user_info.phone,
		uuid: user_info.uuid
	}
	$.ajax({
		url: apicom + "mobile/after/share/share",
		type: 'POST',
		dataType: 'json',
		data: dataObj,
		success: function(result) {
			var res;
			if (typeof(result) == "string") {
				res = JSON.parse(result);
			} else {
				res = result
			}
			if (res.code == 1) {

				//需要进行的操作
			}
		},
		error: function() {
			//提示
			layer.open({
				content: '服务器连接失败',
				skin: 'msg',
				time: 2 //2秒后自动关闭
			});
		}
	})
}


if (mark == 1) {
	setupWebViewJavascriptBridge(function(bridge) {
		bridge.registerHandler('clientToJsShare', function(data, resp) {

			shareCallBack(topicId)
		})
	})
} else if (mark == 0) {
	function clientToJsShare() {

		shareCallBack(topicId)
	}
}